package cn.wjh.business.controller;

import cn.wjh.business.domain.Language;
import cn.wjh.business.service.ILanguageService;
import cn.wjh.common.annotation.Log;
import cn.wjh.common.core.controller.BaseController;
import cn.wjh.common.core.domain.AjaxResult;
import cn.wjh.common.core.page.TableDataInfo;
import cn.wjh.common.enums.BusinessType;
import cn.wjh.common.utils.poi.ExcelUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 翻译语种Controller
 *
 * @author jiraWu
 * @date 2023-11-30
 */
@RestController
@RequestMapping("/business/Language")
public class LanguageController extends BaseController {
    @Resource
    private ILanguageService languageService;

    /**
     * 查询翻译语种列表
     */
    @PreAuthorize("@ss.hasPermi('business:Language:list')")
    @GetMapping("/list")
    @Cacheable(value = "languageListCache", keyGenerator = "cacheKeyGenerator")
    public TableDataInfo list(Language language) {
        startPage();
        List<Language> list = languageService.selectLanguageList(language);
        return getDataTable(list);
    }

    /**
     * 获取全部语言
     */
    @GetMapping("/selectList")
    @Cacheable(value = "languageListCache", keyGenerator = "cacheKeyGenerator")
    public AjaxResult selectList() {
        List<Language> list = languageService.selectList();
        return AjaxResult.success(list);
    }

    /**
     * 导出翻译语种列表
     */
    @PreAuthorize("@ss.hasPermi('business:Language:export')")
    @Log(title = "翻译语种", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Language language) {
        List<Language> list = languageService.selectLanguageList(language);
        ExcelUtil<Language> util = new ExcelUtil<Language>(Language.class);
        util.exportExcel(response, list, "翻译语种数据");
    }

    /**
     * 获取翻译语种详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:Language:query')")
    @GetMapping(value = "/{languageId}")
    @Cacheable(value = "languageListCache", keyGenerator = "cacheKeyGenerator")
    public AjaxResult getInfo(@PathVariable("languageId") Long languageId) {
        return success(languageService.selectLanguageByLanguageId(languageId));
    }

    /**
     * 新增翻译语种
     */
    @PreAuthorize("@ss.hasPermi('business:Language:add')")
    @Log(title = "翻译语种", businessType = BusinessType.INSERT)
    @CacheEvict(value = "languageListCache", allEntries = true)
    @PostMapping
    public AjaxResult add(@RequestBody Language language) {
        return toAjax(languageService.insertLanguage(language));
    }

    /**
     * 修改翻译语种
     */
    @PreAuthorize("@ss.hasPermi('business:Language:edit')")
    @Log(title = "翻译语种", businessType = BusinessType.UPDATE)
    @CacheEvict(value = "languageListCache", allEntries = true)
    @PutMapping
    public AjaxResult edit(@RequestBody Language language) {
        return toAjax(languageService.updateLanguage(language));
    }

    /**
     * 删除翻译语种
     */
    @PreAuthorize("@ss.hasPermi('business:Language:remove')")
    @Log(title = "翻译语种", businessType = BusinessType.DELETE)
    @DeleteMapping("/{languageIds}")
    @CacheEvict(value = "languageListCache", allEntries = true)
    public AjaxResult remove(@PathVariable List<Long> languageIds) {
        return toAjax(languageService.deleteLanguageByLanguageIds(languageIds));
    }
}
